package com.cff.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.codec.binary.Base64;


public class FileUtil {
	private static Properties confProperties = null;
	
	public static String confPath = "conf_loc.properties";
	
	public static Properties getInstance(){
		if(confProperties == null){
			try {
				confProperties = loadProperties();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return confProperties;
	}
	
	public static String getFileContent(String filePath){
		String file = "";
		try{
			FileInputStream fis=new FileInputStream(filePath);  
			ByteArrayOutputStream baos = new ByteArrayOutputStream();  
			byte[] buf = new byte[1024];  
			int len=0;  
			while((len=fis.read(buf))!=-1){  
			    baos.write(buf,0,len);  
			}  
			byte[] data = baos.toByteArray();  
			file= Base64.encodeBase64String(data);  
			fis.close();  
			baos.close();  
			}catch(Exception e){
				e.printStackTrace();
			}
		return file;
	}
	
	public static String getConfPath() {
		return confPath;
	}

	public static void setConfPath(String confPath) {
		FileUtil.confPath = confPath;
	}

	public static Properties loadProperties() throws IOException{
		Properties pro = new Properties();
		InputStream in = FileUtil.class.getResourceAsStream("/"+confPath);
		InputStreamReader is = new InputStreamReader(in,"UTF-8");
		pro.load(is);
		in.close();
		return pro;
	}
}
